package com.interfaces;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.web.Json;
import com.common.web.ReturnCodes;
import com.core.journey.bo.JourneyBo;
import com.core.journey.dto.JourneySearchCondition;
import com.core.journey.pojo.Journey;
import com.core.visitor.bo.VisitorBo;
import com.util.DataBaseUtil;
import com.util.GsonUtil;
import com.util.exception.ComRuntimeException;
import com.util.exception.ComSystemException;
import com.util.framework.BaseController;

@Controller
public class JourneyController extends BaseController{
	protected Logger log= Logger.getLogger(this.getClass());
	@Resource
	private VisitorBo visitorBo;
	@Resource
	private JourneyBo journeyBo;
	
	public static final String URL_ENTITY = "/journey";
	public static final String URL_ADD_UPDATE = URL_ENTITY + "/add_update";
	public static final String URL_UPDATE = URL_ENTITY + "/update";
	public static final String URL_DEL = URL_ENTITY + "/del";
	public static final String URL_DB_SEARCH = URL_ENTITY+"/search";
	
	@ResponseBody
	@RequestMapping(value=URL_DB_SEARCH,method=RequestMethod.POST,produces={"application/json;charset=utf-8"})
	public Object searchData(JourneySearchCondition sc, HttpSession session){
		String msg="";
		List<Journey>data = journeyBo.getJourneyList(sc);
		DataBaseUtil<Journey> db = new DataBaseUtil<Journey>();
		int totalCount = data.size();
		if (data.size()==0) {
			msg="{\"draw\":"+sc.getDraw()+",\"recordsTotal\":0,\"recordsFiltered\":0,\"data\": []}";
		}else{
			db.setTotalCount(totalCount);
			db.setRecordsFiltered(totalCount);
			db.setData(data);
			msg = GsonUtil.gson.toJson(db);
		}
		return msg;
	}
	
	@ResponseBody
	@RequestMapping(value = URL_ADD_UPDATE, method = RequestMethod.POST)
	public Json add(JourneySearchCondition sc, HttpSession session) {
		Json json = new Json();
		try {
			  journeyBo.add(sc);
			  json.setSuccess(true);
			  json.setReturnCode(0);
		} catch (ComRuntimeException e) {
			json = new Json(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... "
					+ e.getMessage());
		}
		return json;
	}
	
	@ResponseBody
	@RequestMapping(value = URL_UPDATE, method = RequestMethod.POST,produces={"application/json;charset=utf-8"})
	public Json update(JourneySearchCondition sc, HttpSession session) {
		Json json = new Json();
		try {
			Journey tempJourney=journeyBo.getById(sc);
			if (tempJourney!=null) {
				journeyBo.update(sc);
				json.setSuccess(true);
				json.setReturnCode(0);
			}
		} catch (ComRuntimeException e) {
			json = new Json(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... "
					+ e.getMessage());
		}
		return json;
	}
	@ResponseBody
	@RequestMapping(value = URL_DEL, method = RequestMethod.POST,produces={"application/json;charset=utf-8"})
	public Json del(JourneySearchCondition sc, HttpSession session) {
		Json json = new Json();
		try {
			Journey tempJourney=journeyBo.getById(sc);
			if (tempJourney!=null) {
				journeyBo.del(sc);
				json.setSuccess(true);
				json.setReturnCode(0);
			}
		} catch (ComRuntimeException e) {
			json = new Json(e.getErrorCode(), e.getMessage());
		} catch (ComSystemException e) {
			log.error("error", e);
			json = new Json(e.getErrorCode(), "系统忙... " + e.getMessage());
		} catch (Throwable e) {
			log.error("error", e);
			json = new Json(ReturnCodes.SYSTEM_EXCEPTION, "系统忙... "
					+ e.getMessage());
		}
		return json;
	}
}
